<html>
<head><meta charset="utf-8"><title>methods that consume self · project-error-handling · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/index.html">project-error-handling</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html">methods that consume self</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232885427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232885427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Gutoski <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232885427">(Apr 02 2021 at 10:45)</a>:</h4>
<p>Hi folks. Any recommendations for the following?  I have a struct method that consumes <code>self</code> and returns <code>Self</code>, kinda like a builder pattern:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">fn</span> <span class="nf">consume_self</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// lots of important stuff</span>
<span class="w">    </span><span class="bp">self</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Now I want to change <code>consume_self</code> so that it might fail.  For example, let's suppose that <code>consume_self</code> needs to deserialize something.  A naive design might do this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">consume_self</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">bytes</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="nc">bincode</span>::<span class="nb">Result</span><span class="o">&lt;</span><span class="bp">Self</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bincode</span>::<span class="n">deserialize</span><span class="p">(</span><span class="n">bytes</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span><span class="c1">// do something important with data</span>
<span class="w">  </span><span class="nb">Ok</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Problem: if there's an error then I lose my <code>self</code>, which is bad.  I can think of several solutions but none of them is a knock-down win.  Any recommendations?</p>



<a name="232885853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232885853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232885853">(Apr 02 2021 at 10:51)</a>:</h4>
<p>what is your top option currently?</p>



<a name="232886328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232886328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Gutoski <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232886328">(Apr 02 2021 at 10:58)</a>:</h4>
<p>One idea is to bundle <code>Self</code> into the returned error:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">consume_self</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">bytes</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="bp">Self</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="bp">Self</span><span class="p">,</span><span class="w"> </span><span class="n">bincode</span>::<span class="n">Error</span><span class="p">)</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bincode</span>::<span class="n">deserialize</span><span class="p">(</span><span class="n">bytes</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="n">res</span><span class="p">.</span><span class="n">is_err</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nb">Err</span><span class="p">((</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">.</span><span class="n">unwrap_err</span><span class="p">()));</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>
<span class="w">  </span><span class="c1">// important stuff</span>
<span class="w">  </span><span class="bp">self</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This way I can recover <code>self</code> even if there's an error.  It's a bit ugly and I don't know yet how ergonomic it is for the caller.</p>



<a name="232886744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232886744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Gutoski <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232886744">(Apr 02 2021 at 11:02)</a>:</h4>
<p>Another idea is to return a tuple:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">consume_self</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">bytes</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="p">(</span><span class="bp">Self</span><span class="p">,</span><span class="w"> </span><span class="nb">Result</span><span class="o">&lt;</span><span class="p">(),</span><span class="w"> </span><span class="n">bincode</span>::<span class="n">Error</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bincode</span>::<span class="n">deserialize</span><span class="p">(</span><span class="n">bytes</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="n">res</span><span class="p">.</span><span class="n">is_err</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>
<span class="w">  </span><span class="c1">// important stuff</span>
<span class="w">  </span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="nb">Ok</span><span class="p">(()))</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="232887488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232887488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Gutoski <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232887488">(Apr 02 2021 at 11:12)</a>:</h4>
<p>I have complete control over the design.  Another more exotic idea is to add a <code>Result</code> member to <code>Foo</code> and rely on the user to check it:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="c1">// important fields</span>
<span class="w">  </span><span class="n">res</span>: <span class="nb">Result</span><span class="o">&lt;</span><span class="p">(),</span><span class="n">bincode</span>::<span class="n">Error</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">fn</span> <span class="nf">consume_self</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">bytes</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="bp">self</span><span class="p">.</span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bincode</span>::<span class="n">deserialize</span><span class="p">(</span><span class="n">bytes</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="c1">// handle error and other important stuff</span>
<span class="w">    </span><span class="bp">self</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="232888013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232888013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232888013">(Apr 02 2021 at 11:20)</a>:</h4>
<p>Does it have to be a builder pattern function? You might also do <code>foo(&amp;mut self, bytes: &amp;[u8]) -&gt; Result&lt;(), bincode::Error&gt;</code></p>



<a name="232888180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232888180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232888180">(Apr 02 2021 at 11:22)</a>:</h4>
<p>if you just want builder pattern for ergonomic reasons, I would stick to the "naive" version that returns <code>bincode::Result&lt;Self&gt;</code>, and use the <code>&amp;mut self</code> version for applications that don't want to lose <code>self</code> even in case of an error</p>



<a name="232888855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232888855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Gutoski <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232888855">(Apr 02 2021 at 11:31)</a>:</h4>
<p>Thanks for the comment!  In my application the user will always want to keep self in case of error.  I'm building a finite state machine that uses Rust's type system to enforce state transitions, so I really want to consume <code>self</code>.  A failed <code>deserialize</code> is really just another state transition.  Another idea I did not explain due to complexity is to modify the state machine design to handle the error that way.  Feel free to post more ideas!</p>



<a name="232889842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232889842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232889842">(Apr 02 2021 at 11:45)</a>:</h4>
<blockquote>
<p>I'm building a finite state machine that uses Rust's type system to enforce state transitions, so I really want to consume self. A failed deserialize is really just another state transition.</p>
</blockquote>
<p>In that case, it sounds like you want the <code>Result&lt;Self, (Self, bincode::Error)&gt;</code> version, since you can use special types in the two branches to encode the typestate</p>



<a name="232889916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232889916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232889916">(Apr 02 2021 at 11:46)</a>:</h4>
<p>in particular, instead of <code>(Self, bincode::Error)</code> it could be <code>ErrorType</code> which is a newtype struct around <code>(Self, bincode::Error)</code> or the parts of <code>Self</code> that are still valid</p>



<a name="232899475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232899475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232899475">(Apr 02 2021 at 13:39)</a>:</h4>
<p>Yea, I was gonna say this sounds like how downcast uses result</p>



<a name="232899499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232899499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232899499">(Apr 02 2021 at 13:39)</a>:</h4>
<p>Where it either returns the concrete type of it succeeds or the original box if it fails</p>



<a name="232899525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232899525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232899525">(Apr 02 2021 at 13:39)</a>:</h4>
<p>Not an uncommon pattern in std</p>



<a name="232899612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232899612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232899612">(Apr 02 2021 at 13:40)</a>:</h4>
<p>I think hashmap has a similar api that wraps the key and value in an error type</p>



<a name="232899658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/methods%20that%20consume%20self/near/232899658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/methods.20that.20consume.20self.html#232899658">(Apr 02 2021 at 13:40)</a>:</h4>
<p>So if you need to return an informative error type you might want to follow they example instead</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>